#include "dbrowser.h"
#include <QDesktopWidget>
#include <QTextEdit>

DBrowser::DBrowser(QString homedir, QWidget *parent) :
    QWidget(parent)
{
    //Depending on the screen size we use full screen or not. Assuming a small screen is a cellular device
    QDesktopWidget *desk=QApplication::desktop();
    bool mobile_device=false;
    if (desk->screenGeometry().height()<900 && desk->screenGeometry().width()<900){  //Probably a mobile device
        setWindowState(Qt::WindowFullScreen);
        mobile_device=true;
    }
    else if (DDeviceInformation::isTouchable()){
        setWindowState(Qt::WindowMaximized);
    }

    QFile qss(DDeviceInformation::getStyleName());
    //qDebug()<< DDeviceInformation::getStyleName();
    if( ! qss.open( QIODevice::ReadOnly ) )
        qDebug("Unable to open qss file");
    else if( qss.isOpen() )
    {
        QString styleSheet = QLatin1String(qss.readAll());
        setStyleSheet( styleSheet );
        this->setStyleSheet(styleSheet);
    }

    layout= new QVBoxLayout();
    layout->setMargin(3);
    layout->setSpacing(2);
    lblLocation=new QLabel;
    lblLocation->setObjectName("lblLocation");
    bButtons=new BrowserButtons();

    fBrowser = new FileBrowser(homedir,this);
    contextMenu = new ContextMenu();
    fBrowser->setContextMenu(contextMenu);
    lblLocation->setText(fBrowser->getCurrentPath());
    lblLocation->setWordWrap(true);
    //lblLocation->setStyleSheet("QLabel {color: #282828; background-color: #E3E4E5; border: 1px solid grey;}");
    connect(bButtons->tbView,SIGNAL(clicked()),this,SLOT(changeView()));

    if (mobile_device){
        layout->addWidget(lblLocation);        
        layout->addWidget(fBrowser);
        layout->addWidget(bButtons);
        bButtons->setMobileGUI();        
        connect(fBrowser->listViewWidget(),SIGNAL(clicked(QModelIndex)),fBrowser,SLOT(openResource()));
    }
    else {
        layout->addWidget(bButtons);
        layout->addWidget(lblLocation);
        layout->addWidget(fBrowser);
        lblLocation->setMinimumHeight(20);
        bButtons->setDesktopGUI();
        connect(fBrowser->listViewWidget(),SIGNAL(doubleClicked(QModelIndex)),fBrowser,SLOT(openResource()));
        connect(fBrowser->tableViewWidget(),SIGNAL(doubleClicked(QModelIndex)),fBrowser,SLOT(openResource()));
        connect(bButtons->tbExit,SIGNAL(clicked()),this,SLOT(exitRequested()));

        iconViewMode=new QAction(QIcon(":/icons32/icon-view"),tr("Icons"),this);
        listViewMode=new QAction(QIcon(":/icons32/list-view"),tr("List"),this);
        detailViewMode=new QAction(QIcon(":/icons32/list-view"),tr("Detail"),this);
        bButtons->tbView->addAction(iconViewMode);
        bButtons->tbView->addAction(listViewMode);
        bButtons->tbView->addAction(detailViewMode);

        connect(bButtons->tbView,SIGNAL(triggered(QAction*)),this,SLOT(changeViewRequested(QAction*)));
    }
    setLayout(layout);

    connect(fBrowser,SIGNAL(changeDirectory(QString)),lblLocation,SLOT(setText(QString)));
    connect(bButtons->tbPrev,SIGNAL(clicked()),fBrowser,SLOT(goBack()));
    connect(bButtons->tbUp,SIGNAL(clicked()),fBrowser,SLOT(goUp()));
    connect(bButtons->tbNext,SIGNAL(clicked()),fBrowser,SLOT(goForward()));
    connect(bButtons->tbHome,SIGNAL(clicked()),fBrowser,SLOT(goHome()));
    connect(fBrowser, SIGNAL(setBackButtonEnabled(bool)),bButtons->tbPrev,SLOT(setEnabled(bool)));
    connect(fBrowser, SIGNAL(setNextButtonEnabled(bool)),bButtons->tbNext,SLOT(setEnabled(bool)));
    connect(fBrowser, SIGNAL(setUpButtonEnabled(bool)),bButtons->tbUp,SLOT(setEnabled(bool)));
    connect(bButtons->tbFull,SIGNAL(clicked()),this,SLOT(maximizeRequested()));
    connect(this,SIGNAL(mapReady(QMap<qint64,int>*)),fBrowser,SLOT(setMap(QMap<qint64,int>*)));

    //Shortcut definitions
    QShortcut *scOpen = new QShortcut(QKeySequence(tr("Ctrl+o","Open")),this);
    connect(scOpen,SIGNAL(activated()),fBrowser,SLOT(openResource()));
    QShortcut *scCopy = new QShortcut(QKeySequence(tr("Ctrl+c","Copy")),this);
    connect(scCopy,SIGNAL(activated()),fBrowser,SLOT(copyResourceToBuffer()));
    QShortcut *scCut = new QShortcut(QKeySequence(tr("Ctrl+x","Cut")),this);
    connect(scCut,SIGNAL(activated()),fBrowser,SLOT(cutResourceToBuffer()));
    QShortcut *scPaste = new QShortcut(QKeySequence(tr("Ctrl+v","Paste")),this);
    connect(scPaste,SIGNAL(activated()),fBrowser,SLOT(pasteResource()));
    QShortcut *scBack = new QShortcut(QKeySequence(tr("Alt+Left","Back")),this);
    connect(scBack,SIGNAL(activated()),fBrowser,SLOT(goBack()));
    QShortcut *scForward = new QShortcut(QKeySequence(tr("Alt+Right","Forward")),this);
    connect(scForward,SIGNAL(activated()),fBrowser,SLOT(goForward()));
    QShortcut *scUp = new QShortcut(QKeySequence(tr("Alt+Up","Up")),this);
    connect(scUp,SIGNAL(activated()),fBrowser,SLOT(goUp()));
    QShortcut *scHome = new QShortcut(QKeySequence(tr("Home","Home")),this);
    connect(scHome,SIGNAL(activated()),fBrowser,SLOT(goHome()));
    QShortcut *scDelete = new QShortcut(QKeySequence(tr("Del","Delete")),this);
    connect(scDelete,SIGNAL(activated()),fBrowser,SLOT(deleteResourceRequest()));
    QShortcut *scEnter = new QShortcut(QKeySequence(tr("Enter","Enter")),this);
    connect(scEnter,SIGNAL(activated()),fBrowser,SLOT(openResource()));
    QShortcut *scIntro = new QShortcut(QKeySequence(tr("Return","Enter")),this);
    connect(scIntro,SIGNAL(activated()),fBrowser,SLOT(openResource()));
    QShortcut *scViewIcons = new QShortcut(QKeySequence(tr("Ctrl+1","View icons")),this);
    connect(scViewIcons,SIGNAL(activated()),fBrowser,SLOT(setIconViewConfiguration()));
    QShortcut *scViewList = new QShortcut(QKeySequence(tr("Ctrl+2","View list")),this);
    connect(scViewList,SIGNAL(activated()),fBrowser,SLOT(setListViewConfiguration()));
    QShortcut *scViewDetail = new QShortcut(QKeySequence(tr("Ctrl+3","View detail")),this);
    connect(scViewDetail,SIGNAL(activated()),fBrowser,SLOT(setDetailViewConfiguration()));
}

void DBrowser::exitRequested(){
    emit exit();
}

void DBrowser::maximizeRequested(){
    bButtons->tbFull->toggle();
    if(bButtons->tbFull->isChecked())
        bButtons->tbFull->setChecked(false);
    else
        bButtons->tbFull->setChecked(true);
    emit maximize();
}

void DBrowser::changeViewRequested(QAction * view){
    if ((view)==iconViewMode && fBrowser->viewMode()!=FileBrowser::icon){
        fBrowser->changeView(FileBrowser::icon);
        bButtons->tbView->setIcon(QIcon(":/icons32/icon-view"));
    }
    else if(view==listViewMode && fBrowser->viewMode()!=FileBrowser::list){
        fBrowser->changeView(FileBrowser::list);
        bButtons->tbView->setIcon(QIcon(":/icons32/list-view"));
    }
    else if(view==detailViewMode && fBrowser->viewMode()!=FileBrowser::detail){
        fBrowser->changeView(FileBrowser::detail);
        bButtons->tbView->setIcon(QIcon(":/icons32/list-view"));
    }
}


void DBrowser::changeView(){
    if (fBrowser->viewMode()==FileBrowser::icon){
        fBrowser->changeView(FileBrowser::list);
        bButtons->tbView->setIcon(QIcon(":/icons32/list-view"));
    }
    else if (fBrowser->viewMode()==FileBrowser::list){
        fBrowser->changeView(FileBrowser::detail);
        bButtons->tbView->setIcon(QIcon(":/icons32/list-view"));
    }
    else{
        fBrowser->changeView(FileBrowser::icon);
        bButtons->tbView->setIcon(QIcon(":/icons32/icon-view"));
    }
    bButtons->tbView->setIconSize(QSize(32,32));
}


void DBrowser::setItemWidget(QString path, QWidget * w){
    fBrowser->setItemWidget(path, w);
}

void DBrowser::setOverlayStatus(QString path, int status){
    fBrowser->setOverlayStatus(path,status);
}

void DBrowser::removeOverlayStatus(QString path){
    fBrowser->setOverlayStatus(path);
}

QFileSystemModel *DBrowser::getDirModel(){
    return fBrowser->getDirModel();
}

void DBrowser::addToolButtonButton(QToolButton *btn){
    bButtons->addWidget(btn);
}

/*void DBrowser::removeFromToolButton(QToolButton *btn){
    QToolButton *b=bButtons->findChild(btn->objectName());
}
*/

void DBrowser::addContextMenuAction(QAction *action){
    contextMenu->addContextMenuAction(action);
}

void DBrowser::addContextMenuMenu(QMenu *menu){
    contextMenu->addContextMenuMenu(menu);
}

void DBrowser::addContextMenuSeparator(){
    contextMenu->addContextMenuSeparator();

}

void DBrowser::removeFromContextMenu(int index){
    contextMenu->removeFromContextMenu(contextMenu->contextMenuActions().at(index));
}
/*
void DBrowser::setContextMenuActionEnabled(QAction *action, bool enable){
    contextMenu->contextMenuActions().at(contextMenu->contextMenuActions().indexOf(action))->setEnabled(enable);
}

void DBrowser::setContextMenuActionVisible(QAction *action, bool enable){
    contextMenu->contextMenuActions().at(contextMenu->contextMenuActions().indexOf(action))->setVisible(enable);
}
*/
